const geohash = require('ngeohash');
const gps2Ue4 = require('./GPSToUe4');

const { DATA_COUNTS, PRECISION, GPS_BOUNDARY } = require('../config');

let x1 = GPS_BOUNDARY.rightBottom[0]; // 109
let x2 = GPS_BOUNDARY.leftTop[0]; // 108
let y1 = GPS_BOUNDARY.leftTop[1]; // 34.59
let y2 = GPS_BOUNDARY.rightBottom[1]; // 34.42

//gps
let geojsonData = [];
//ue4
let Ue4Data = [];
//hash
let hashData = [];
//center
let centerData = [];
//ue4center
let ue4centerData = [];


for(let i=0;i<DATA_COUNTS;i++){
    //随机坐标值
    let x = Math.random()*(x1-x2)+x2;
    let y = Math.random()*(y1-y2)+y2;
    console.log('------------------------', x, y)

    // console.log(x, y, "gps");
    geojsonData.push([x,y]);
    // console.log(gps2Ue4([x,y]));
    Ue4Data.push(gps2Ue4([x,y]));

    //hash值
    let hash = geohash.encode(y, x, PRECISION);
    hashData.push(hash);
    // console.log(hash, 'xxxx')

    //反解hash值
    let decodeHash = geohash.decode_bbox(hash);
    //计算hash边界值
    let [minlat, minlon, maxlat, maxlon] = decodeHash;

    //中心点坐标
    let centerGpsX = (maxlon + minlon) / 2;
    let centerGpsY = (maxlat + minlat)/2;
    centerData.push([centerGpsX,centerGpsY]);
    //ue4中心点坐标
    ue4centerData.push(gps2Ue4([centerGpsX,centerGpsY]));
}


/**
 * 格式化高德请求参数
 * @param {Array} geojsonDataInput 
 */
function formatGaodeParamFunc(geojsonDataInput){
    let gpsStringList = geojsonDataInput.map(([x,y]) => {
        return x+','+y;
    });
    
    let paramList = []; //请求参数

    let item = '';
    let paramListIndex = 0;
    const SLICE_LENGTH = 20; //最大请求的坐标点个数
    for(let i=0;i<gpsStringList.length;i++) {
        if ( i < SLICE_LENGTH*(paramListIndex+1)) {
            item += i === SLICE_LENGTH*paramListIndex ? gpsStringList[i] : '|' + gpsStringList[i];
            if (i === gpsStringList.length - 1) {
                paramList.push(item);
            }
        } else {
            paramList.push(item);
            paramListIndex++;
            item = gpsStringList[i];
        }
    }
    console.log('格式化高德数据：',paramList)
    return paramList;
}


let formatGaodeParam = formatGaodeParamFunc(geojsonData);

module.exports = {
    geojsonData,
    formatGaodeParam,
    Ue4Data,
    hashData,
    centerData,
    ue4centerData
}
